//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.view
{
	import __AS3__.vec.Vector;
	
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	import reader.ApplicationFacade;
	import reader.core.ReaderModelLocator;
	import reader.events.BlogLabelEvent;
	import reader.events.BlogSourceEvent;
	import reader.model.IDataProxy;
	import reader.model.IUserProxy;
	import reader.model.vo.BlogLabelVO;
	import reader.model.vo.BlogSourceVO;
	import reader.model.vo.TitleWindowVO;
	import reader.model.vo.UserVO;
	import reader.utils.OPMLUtils;
	import reader.view.components.SettingPanel;

	public class SettingPanelMediator extends BaseApplicationMediator implements IMediator
	{
		////////////////////////////////////////////////////////////////////////////////
		//public properties
		public static const NAME:String = "SettingPanelMediator";
		
		public function get settingPanel():SettingPanel
		{
			return this.viewComponent as SettingPanel;
		}
		
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		private var dataProxy:IDataProxy;
		private var userProxy:IUserProxy;
		private var readerModelLocator:ReaderModelLocator = ReaderModelLocator.instance;
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function SettingPanelMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			
			dataProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.dataProxyName) as IDataProxy;
			userProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.userProxyName) as IUserProxy;
			
			settingPanel.addEventListener(SettingPanel.CLOSE_TYPED_WINDOW, onCloseWindow_handler, false, 0, true);
			settingPanel.addEventListener(SettingPanel.REMOVE_LABELS, onLabelsRemove_handler, false, 0, true);
			settingPanel.addEventListener(SettingPanel.REMOVE_SUBSCRIBES, onRemoveSubscribes_handler, false, 0, true);
			settingPanel.addEventListener(SettingPanel.CHANGE_PREPERENCE, onChangePreference_handler, false, 0, true);
			settingPanel.addEventListener(SettingPanel.OPML_HELP, onOPMLHelp_handler, false, 0, true);
			settingPanel.addEventListener(SettingPanel.OPML_INPORT, onOPMLInport_handler, false, 0, true);
			settingPanel.addEventListener(SettingPanel.OPML_OUTPORT, onOPMLOutport_handler, false, 0, true);
			settingPanel.addEventListener(BlogSourceEvent.UPDATE_BLOG_SOURCE_NAME, onRenameSubscribe_handler);
			settingPanel.addEventListener(BlogSourceEvent.UNSUBSCRIBE, onRemoveSubscribe_handler);
			settingPanel.addEventListener(BlogLabelEvent.LABEL_CREATE, onLabelCreate_handler);
			settingPanel.addEventListener(BlogLabelEvent.LABEL_REMOVE, onLabelRemove_handler);
			settingPanel.addEventListener(BlogSourceEvent.SOURCE_LABEL_CHANGE, onSourceLabelChange_handler);
			
		}
						
		override public function listNotificationInterests():Vector.<String>
		{
			return Vector.<String>([ApplicationFacade.SETTING_UPDATE]);
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				/* case ApplicationFacade.REMOVED_SUBSCRIBE:
					//settingPanel.blogListAllLinkButton.label = settingPanel.blogListAllButtonLabel;
					break;
				case ApplicationFacade.REMOVED_LABEL:
					//settingPanel.blogLabelAllLinkButton.label = settingPanel.blogLabelAllButtonLabel;
					break; */
				case ApplicationFacade.SETTING_UPDATE:
					settingPanel.close();
					break;	
			}
		}
		
		
		
		//////////////////////////////////////////////////////////////////////////////
		//event
		
		private function onCloseWindow_handler(e:Event):void
		{
			dataProxy = null;
			userProxy = null;
			readerModelLocator = null;
			
			settingPanel.removeEventListener(SettingPanel.CLOSE_TYPED_WINDOW, onCloseWindow_handler);
			settingPanel.removeEventListener(SettingPanel.REMOVE_LABELS, onLabelsRemove_handler);
			settingPanel.removeEventListener(SettingPanel.REMOVE_SUBSCRIBES, onRemoveSubscribes_handler);
			settingPanel.removeEventListener(SettingPanel.CHANGE_PREPERENCE, onChangePreference_handler);
			settingPanel.removeEventListener(SettingPanel.OPML_HELP, onOPMLHelp_handler);
			settingPanel.removeEventListener(SettingPanel.OPML_INPORT, onOPMLInport_handler);
			settingPanel.removeEventListener(SettingPanel.OPML_OUTPORT, onOPMLOutport_handler);
			settingPanel.removeEventListener(BlogSourceEvent.UPDATE_BLOG_SOURCE_NAME, onRenameSubscribe_handler);
			settingPanel.removeEventListener(BlogSourceEvent.UNSUBSCRIBE, onRemoveSubscribe_handler);
			settingPanel.removeEventListener(BlogLabelEvent.LABEL_CREATE, onLabelCreate_handler);
			settingPanel.removeEventListener(BlogLabelEvent.LABEL_REMOVE, onLabelRemove_handler);
			settingPanel.removeEventListener(BlogSourceEvent.SOURCE_LABEL_CHANGE, onSourceLabelChange_handler);
			
			facade.removeCommand(ApplicationFacade.OPML_EXECUTE)
			facade.removeCommand(ApplicationFacade.REMOVE_LABEL);
			facade.removeMediator(NAME);
			
			settingPanel.close();
		}
		
		private function onChangePreference_handler(e:Event):void
		{
			var user:UserVO = readerModelLocator.user;
			/* user.culture = settingPanel.preferencesLanguage.selectedItem.data;
			user.displayIndex = settingPanel.preferencesStartpage.selectedItem.data; 
			user.isRollRead = settingPanel.preferencesScrollTracking.selected;
			user.isGuide = settingPanel.preferencesGuidePanel.selected;
			user.isMarkAll = settingPanel.preferencesExternelMarkAll.selected;
			
			userProxy.updateUserSetting(user);*/
		}
		
		private function onLabelCreate_handler(e:BlogLabelEvent):void
		{
			var labelFetch:TitleWindowVO = new TitleWindowVO(settingPanel, dataProxy, Vector.<BlogSourceVO>([e.blogSource]));
			sendNotification(ApplicationFacade.LABEL_CREATE, labelFetch);
		}
		
		private function onLabelRemove_handler(e:BlogLabelEvent):void
		{
			var labelFetch:TitleWindowVO = new TitleWindowVO(settingPanel, dataProxy, null, Vector.<BlogLabelVO>([e.blogLabel]));
			sendNotification(ApplicationFacade.REMOVE_LABEL, labelFetch);
		}
		
		private function onLabelsRemove_handler(e:Event):void
		{
			if(settingPanel.blogLabelList.selectedItems.length > 0)
			{
				var labelFetch:TitleWindowVO = new TitleWindowVO(settingPanel, dataProxy, null, Vector.<BlogLabelVO>(settingPanel.blogLabelList.selectedItems));
				sendNotification(ApplicationFacade.REMOVE_LABEL, labelFetch);
			}			
		}
		
		private function onSourceLabelChange_handler(e:BlogSourceEvent):void
		{
			dataProxy.updateBlogSource(e.blogSource);
		}
		
		private function onRenameSubscribe_handler(e:BlogSourceEvent):void
		{
			var labelFetch:TitleWindowVO = new TitleWindowVO(settingPanel, dataProxy, Vector.<BlogSourceVO>([e.blogSource]));
			sendNotification(ApplicationFacade.RENAME_SUBSCRIBE, labelFetch);
		}
		
		private function onRemoveSubscribe_handler(e:BlogSourceEvent):void
		{
			var labelFetch:TitleWindowVO = new TitleWindowVO(settingPanel, dataProxy, Vector.<BlogSourceVO>([e.blogSource]));
			sendNotification(ApplicationFacade.REMOVE_SUBSCRIBE, labelFetch);
		}
		
		private function onRemoveSubscribes_handler(e:Event):void
		{
			if(settingPanel.blogSourceList.selectedItems.length > 0)
			{
				var labelFetch:TitleWindowVO = new TitleWindowVO(settingPanel, dataProxy, Vector.<BlogSourceVO>(settingPanel.blogSourceList.selectedItems));
				sendNotification(ApplicationFacade.REMOVE_SUBSCRIBE, labelFetch);
			}
			
		}
		
		private function onOPMLHelp_handler(e:Event):void
		{
			//TODO:
		}
		
		private function onOPMLInport_handler(e:Event):void
		{
			sendNotification(ApplicationFacade.OPML_EXECUTE, dataProxy, OPMLUtils.INPORT_OPML);
		}
		
		private function onOPMLOutport_handler(e:Event):void
		{
			sendNotification(ApplicationFacade.OPML_EXECUTE, null, OPMLUtils.OUTPORT_OPML);
		}
	}
}